export RAW=1

if [ $# -ge 2 ] 
then
	if [ "$1" = "-R" ]
	then export RAW=0
	fi
	shift
fi

export FILE=$1
export CLASS=$(basename $1 .php)

(
	grep "@var" $FILE  | sed "s/^[\t ][\t ]*\* @var //g" 
	if [ $RAW -eq 0 ]
	then 
		sed -f remccmds3.sed $FILE | uniq | \
			sed ':a;N;$!ba;s/.*public function relations[ \t]*()[^{]*{\([^}]*\)}.*/\1/g' | \
			grep "=>" | sed -e "s/=>/,/g" -e "s/'//g" -e "s/[ \t][ \t]*//g" | \
			awk 'BEGIN{ FS=","; OFS=" " }{ print "H" $3, "$" $1 }'

	fi
) | \
	awk -vQUOTE="'" -vCLASS=$CLASS 'BEGIN { 
		attributes=""
		virg="" 
		print "<?php\n"
		print "class H" CLASS " extends CModel  {"
	}
	{ 
		print "\t/**";
		print "\t * @var", $1, $2;
		print "\t * @soap";
		print "\t */"
		print "\tpublic", $2, ";\n"; 
		attributes=attributes virg QUOTE substr($2, 2) QUOTE
		virg=","
	} END {
		print "\tpublic function attributeNames() {"
		print "\t\treturn array (" attributes ");"
		print "\t}\n"
		
		split(attributes, s, ",");
		print "\tpublic function __toString() {"
		print "\t\treturn \"H" CLASS " [\" ."
		virg=""
		for (i in s) {
			attribute=substr(s[i], 2, length(s[i])-2);
			print "\t\t\t\"" virg attribute "=\" . $this->" attribute " ."
			virg=", "
		}
		print "\t\t\t\"]\";"
		print "\t}"
		
		print "}"
	}'
